package ${config.basePackage}.entity.query;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Date;

import ${config.basePackage}.query.QueryColumn;
import ${config.basePackage}.query.QueryExpreEnum;
import ${config.basePackage}.query.QueryOrder;
import ${config.basePackage}.query.QueryPageInfo;

/**
 * <#if tableinfo.tableComment ?? > ${tableinfo.tableComment}  </#if>
 *
 * ${.now?string('yyyy-MM-dd HH:mm:ss')}
 * 
 * @author 工具自动生成       Lv Bu
 *
 */
public class ${tableinfo.tableName?cap_first}Query {
	
	/**
	 * 要显示的列
	 * 
	 */
	private String[] showColumns;

	public String[] getShowColumns() {
		return showColumns;
	}

	public void setShowColumns(String[] showColumns) {
		this.showColumns = showColumns;
	}
	
	
	/**
	 * 分页条件
	 * 
	 */
	private QueryPageInfo pageInfo;

	public QueryPageInfo getQueryPageInfo() {
		return pageInfo;
	}

	public void setPageRow(Integer row) {
		if (pageInfo == null) {
			pageInfo = new QueryPageInfo();
		}
		pageInfo.setRow(row);
	}

	public void setPage(Integer page) {
		if (pageInfo == null) {
			pageInfo = new QueryPageInfo();
		}
		pageInfo.setPage(page);
	}

	/**
	 * 排序条件拼接
	 * 
	 */
	private List<QueryOrder> orders;

	public List<QueryOrder> getOrders() {
		return orders;
	}
	
	<#list tableinfo.columns as column>
	public void add${column.columnName?cap_first}OrderAsc() {
		if (orders == null) {
			orders = new LinkedList<QueryOrder>();
		}
		orders.add(new QueryOrder("${column.dbColumnName}", "asc"));
	}

	public void add${column.columnName?cap_first}OrderDesc() {
		if (orders == null) {
			orders = new LinkedList<QueryOrder>();
		}
		orders.add(new QueryOrder("${column.dbColumnName}", "desc"));
	}

	
	</#list>

	


	
	/**
	 * 查询条件拼接
	 * 
	 */
	private List<QueryColumn> queryColumns;

	public List<QueryColumn> getQueryColumns() {
		return queryColumns;
	}

	/**
	 * 添加空查询以包装子查询
 	 *
	 **/
	public QueryColumn addNullQuery() {
		return this.addNullQuery(null);
	}
	
	public QueryColumn addNullQuery(String mosaic) {
		return this.addQueryColumnToList(mosaic);
	}

	<#-- AND 查询范围 -->
	<#list tableinfo.columns as column>
	/**
	* ${column.columnName} 等于
	*
	*/
	public QueryColumn addEqAnd${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("AND", "${column.dbColumnName}", QueryExpreEnum.EQ, "${column.columnType}", value);
	}
	
	public QueryColumn addChildEqAnd${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("AND", "${column.dbColumnName}", QueryExpreEnum.EQ, "${column.columnType}", value);
		 return queryColumn;
	}
	
	/**
	* ${column.columnName} 不等于
	*
	*/
	public QueryColumn addNeqAnd${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("AND", "${column.dbColumnName}", QueryExpreEnum.NEQ, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildNeqAnd${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("AND", "${column.dbColumnName}", QueryExpreEnum.NEQ, "${column.columnType}", value);
		 return queryColumn;
	}
	
	
	/**
	* ${column.columnName} IsNull查询
	*
	**/
	public QueryColumn addIsNullAnd${column.columnName?cap_first}() {
		return this.addQueryColumnToList("AND", "${column.dbColumnName}",QueryExpreEnum.ISNULL, "${column.columnType}", null);
	}
	
	
	public QueryColumn addChildIsNullAnd${column.columnName?cap_first}(QueryColumn queryColumn) {
		 queryColumn.addChildQueryColumn("AND", "${column.dbColumnName}", QueryExpreEnum.ISNULL, "${column.columnType}", null);
		 return queryColumn;
	}
	
	
	/**
	* ${column.columnName} IsNotNull查询
	*
	**/
	public QueryColumn addIsNotNullAnd${column.columnName?cap_first}() {
		return this.addQueryColumnToList("AND", "${column.dbColumnName}", QueryExpreEnum.ISNOTNULL, "${column.columnType}", null);
	}
	
	public QueryColumn addChildIsNotNullAnd${column.columnName?cap_first}(QueryColumn queryColumn) {
		 queryColumn.addChildQueryColumn("AND", "${column.dbColumnName}", QueryExpreEnum.ISNOTNULL, "${column.columnType}", null);
		 return queryColumn;
	}
	
	
	<#if column.columnType=='String' || column.columnType=='Integer' || column.columnType=='Double' || column.columnType=='Float'>
	<#if column.columnType=='String'>
	/**
	* ${column.columnName} 字符串匹配查询
	*
	*/
	public QueryColumn addLikeAnd${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("AND", "${column.dbColumnName}", QueryExpreEnum.LIKE, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildLikeAnd${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("AND", "${column.dbColumnName}", QueryExpreEnum.LIKE, "${column.columnType}", value);
		 return queryColumn;
	}
	
	</#if>
	/**
	* ${column.columnName} IN查询
	*
	*/
	public QueryColumn addInAnd${column.columnName?cap_first}(${column.columnType}[] value) {
		 return this.addQueryColumnToList("AND", "${column.dbColumnName}", QueryExpreEnum.IN, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildInAnd${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType}[] value) {
		 queryColumn.addChildQueryColumn("AND", "${column.dbColumnName}", QueryExpreEnum.IN, "${column.columnType}", value);
		 return queryColumn;
	}
	
	
	/**
	* ${column.columnName} NOTIN查询
	*
	*/
	public QueryColumn addNotInAnd${column.columnName?cap_first}(${column.columnType}[] value) {
		 return this.addQueryColumnToList("AND", "${column.dbColumnName}", QueryExpreEnum.NOTIN, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildNotInAnd${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType}[] value) {
		 queryColumn.addChildQueryColumn("AND", "${column.dbColumnName}", QueryExpreEnum.NOTIN, "${column.columnType}", value);
		 return queryColumn;
	}
	
	
	</#if>
	<#if column.columnType=='Integer' || column.columnType=='Double' || column.columnType=='Float' || column.columnType=='Date'>
	/**
	* ${column.columnName} GT查询
	*
	*/
	public QueryColumn addGtAnd${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("AND", "${column.dbColumnName}", QueryExpreEnum.GT, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildGtAnd${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("AND", "${column.dbColumnName}", QueryExpreEnum.GT, "${column.columnType}", value);
		 return queryColumn;
	}
	
	
	/**
	* ${column.columnName} LT查询
	*
	*/
	public QueryColumn addLtAnd${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("AND", "${column.dbColumnName}", QueryExpreEnum.LT, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildLtAnd${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("AND", "${column.dbColumnName}", QueryExpreEnum.LT, "${column.columnType}", value);
		 return queryColumn;
	}
	
	/**
	* ${column.columnName} GE查询
	*
	*/
	public QueryColumn addGeAnd${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("AND", "${column.dbColumnName}", QueryExpreEnum.GE, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildGeAnd${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("AND", "${column.dbColumnName}", QueryExpreEnum.GE, "${column.columnType}", value);
		 return queryColumn;
	}
	
	
	/**
	* ${column.columnName} LE查询
	*
	*/
	public QueryColumn addLeAnd${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("AND", "${column.dbColumnName}", QueryExpreEnum.LE, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildLeAnd${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("AND", "${column.dbColumnName}", QueryExpreEnum.LE, "${column.columnType}", value);
		 return queryColumn;
	}
	
	
	</#if>
	</#list>
	
	<#-- OR 查询范围 -->
	<#list tableinfo.columns as column>
	/**
	* ${column.columnName} 等于
	*
	*/
	public QueryColumn addEqOr${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("OR", "${column.dbColumnName}", QueryExpreEnum.EQ, "${column.columnType}", value);
	}
	
	public QueryColumn addChildEqOr${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("OR", "${column.dbColumnName}", QueryExpreEnum.EQ, "${column.columnType}", value);
		 return queryColumn;
	}
	
	/**
	* ${column.columnName} 不等于
	*
	*/
	public QueryColumn addNeqOr${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("OR", "${column.dbColumnName}", QueryExpreEnum.NEQ, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildNeqOr${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("OR", "${column.dbColumnName}", QueryExpreEnum.NEQ, "${column.columnType}", value);
		 return queryColumn;
	}
	
	
	/**
	* ${column.columnName} IsNull查询
	*
	**/
	public QueryColumn addIsNullOr${column.columnName?cap_first}() {
		return this.addQueryColumnToList("OR", "${column.dbColumnName}",QueryExpreEnum.ISNULL, "${column.columnType}", null);
	}
	
	
	public QueryColumn addChildIsNullOr${column.columnName?cap_first}(QueryColumn queryColumn) {
		 queryColumn.addChildQueryColumn("OR", "${column.dbColumnName}", QueryExpreEnum.ISNULL, "${column.columnType}", null);
		 return queryColumn;
	}
	
	
	/**
	* ${column.columnName} IsNotNull查询
	*
	**/
	public QueryColumn addIsNotNullOr${column.columnName?cap_first}() {
		return this.addQueryColumnToList("OR", "${column.dbColumnName}", QueryExpreEnum.ISNOTNULL, "${column.columnType}", null);
	}
	
	public QueryColumn addChildIsNotNullOr${column.columnName?cap_first}(QueryColumn queryColumn) {
		 queryColumn.addChildQueryColumn("OR", "${column.dbColumnName}", QueryExpreEnum.ISNOTNULL, "${column.columnType}", null);
		 return queryColumn;
	}
	
	
	<#if column.columnType=='String' || column.columnType=='Integer' || column.columnType=='Double' || column.columnType=='Float'>
	<#if column.columnType=='String'>
	/**
	* ${column.columnName} 字符串匹配查询
	*
	*/
	public QueryColumn addLikeOr${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("OR", "${column.dbColumnName}", QueryExpreEnum.LIKE, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildLikeOr${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("OR", "${column.dbColumnName}", QueryExpreEnum.LIKE, "${column.columnType}", value);
		 return queryColumn;
	}
	
	</#if>
	/**
	* ${column.columnName} IN查询
	*
	*/
	public QueryColumn addInOr${column.columnName?cap_first}(String value) {
		 return this.addQueryColumnToList("OR", "${column.dbColumnName}", QueryExpreEnum.IN, "String", value);
	}
	
	
	public QueryColumn addChildInOr${column.columnName?cap_first}(QueryColumn queryColumn,String value) {
		 queryColumn.addChildQueryColumn("OR", "${column.dbColumnName}", QueryExpreEnum.IN, "String", value);
		 return queryColumn;
	}
	
	
	/**
	* ${column.columnName} NOTIN查询
	*
	*/
	public QueryColumn addNotInOr${column.columnName?cap_first}(String value) {
		 return this.addQueryColumnToList("OR", "${column.dbColumnName}", QueryExpreEnum.NOTIN, "String", value);
	}
	
	
	public QueryColumn addChildNotInOr${column.columnName?cap_first}(QueryColumn queryColumn,String value) {
		 queryColumn.addChildQueryColumn("OR", "${column.dbColumnName}", QueryExpreEnum.NOTIN, "String", value);
		 return queryColumn;
	}
	
	
	</#if>
	<#if column.columnType=='Integer' || column.columnType=='Double' || column.columnType=='Float' || column.columnType=='Date'>
	/**
	* ${column.columnName} GT查询
	*
	*/
	public QueryColumn addGtOr${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("OR", "${column.dbColumnName}", QueryExpreEnum.GT, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildGtOr${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("OR", "${column.dbColumnName}", QueryExpreEnum.GT, "${column.columnType}", value);
		 return queryColumn;
	}
	
	
	/**
	* ${column.columnName} LT查询
	*
	*/
	public QueryColumn addLtOr${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("OR", "${column.dbColumnName}", QueryExpreEnum.LT, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildLtOr${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("OR", "${column.dbColumnName}", QueryExpreEnum.LT, "${column.columnType}", value);
		 return queryColumn;
	}
	
	/**
	* ${column.columnName} GE查询
	*
	*/
	public QueryColumn addGeOr${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("OR", "${column.dbColumnName}", QueryExpreEnum.GE, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildGeOr${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("OR", "${column.dbColumnName}", QueryExpreEnum.GE, "${column.columnType}", value);
		 return queryColumn;
	}
	
	
	/**
	* ${column.columnName} LE查询
	*
	*/
	public QueryColumn addLeOr${column.columnName?cap_first}(${column.columnType} value) {
		 return this.addQueryColumnToList("OR", "${column.dbColumnName}", QueryExpreEnum.LE, "${column.columnType}", value);
	}
	
	
	public QueryColumn addChildLeOr${column.columnName?cap_first}(QueryColumn queryColumn,${column.columnType} value) {
		 queryColumn.addChildQueryColumn("OR", "${column.dbColumnName}", QueryExpreEnum.LE, "${column.columnType}", value);
		 return queryColumn;
	}
	
	
	</#if>
	</#list>
		
	private QueryColumn addQueryColumnToList(String mosaic) {
		if (queryColumns==null) {
			queryColumns=new ArrayList<QueryColumn>();
		}
		QueryColumn queryColumn=new QueryColumn();
		if (mosaic!=null) {
			queryColumn.setMosaic(mosaic);
		}
		queryColumns.add(queryColumn);
		return queryColumn;
	}
		
	private QueryColumn addQueryColumnToList(String mosaic,String filedName,QueryExpreEnum queryEnum,String filedType,Object value) {
		if (queryColumns==null) {
			queryColumns=new LinkedList<QueryColumn>();
		}
		QueryColumn queryColumn=new QueryColumn(mosaic,filedName,queryEnum,filedType,value);
		queryColumns.add(queryColumn);
		return queryColumn;
	}
	
	
	public void clearQureyColumn() {
		this.queryColumns=null;
	}
}
